package com.qf.collection.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 演示使用迭代器的注意事项
 */
public class IteratorDemo {
    public static void main(String[] args) {

        Collection c1 = new ArrayList();
        c1.add("hello");
        c1.add("world");
        c1.add("hi");

        Iterator iterator = c1.iterator();
        while (iterator.hasNext()){
            // java.util.ConcurrentModificationException
//            c1.add("123");
//            c1.remove("hi");
            Object next = iterator.next();
            System.out.println(next);
        }
        System.out.println(iterator.hasNext());
        //在迭代器对象已经使用完之后，继续调用netx方法会抛出 java.util.NoSuchElementException
        System.out.println(iterator.next());



    }
}
